const { REDIRECT_DOMAIN, REDIRECT_PATH } = require('../../constants/domain');
const { getUserToken, getUserInfo, getMode, getTheme, getStyleMode, getLocationValue } = require('../../biztools/info');
const { DEFAULT_MODE, DEFAULT_THEME, DEFAULT_STYLE_MODE } = require('../../config');

/**
 * 获取链接地址
 * @param {String} type 类型
 * @param {Object} params 参数
 */
const getWebviewUrl = (type, params = {}, appConfig) => {
    const { goodsId, goodsIds, orderNumber, storeId, storeName, title } = params;
    const { latitude, longitude } = getLocationValue();
    const { brandInfo, config } = appConfig;
    console.log('设置信息', config);
    const querys = `redirectType=${type}`;
    const memberParam = `&memberToken=${getUserToken()}&memberId=${getUserInfo().member_id}`;
    const themeParam = `&mode=${getMode() || DEFAULT_MODE}&theme=${(getTheme() || DEFAULT_THEME).replace('#', '')}&styleMode=${getStyleMode() || DEFAULT_STYLE_MODE}`;
    const bizParam = `${goodsId ? '&goodsId=' + goodsId : ''}${goodsIds ? '&goodsIds=' + goodsIds : ''}${orderNumber ? '&orderNumber=' + orderNumber : ''}${storeId ? '&storeId=' + storeId : ''}${storeName ? '&storeName=' + storeName : ''}${config.cart.goodsIds ? '&cartIds=' + config.cart.goodsIds.join(',') : ''}`;
    const locationParam = `&latitude=${latitude}&longitude=${longitude}`;
    const brandParam = `&operationMode=${brandInfo.operationMode}&brandId=${brandInfo.id}&brandName=${brandInfo.name}&allowBooking=${brandInfo.allowBooking}&bookingDays=${brandInfo.bookingDays}`;
    const otherParam = `${title ? '&title=' + title : ''}`;
    return `${REDIRECT_DOMAIN}${REDIRECT_PATH}?${querys}${themeParam}${memberParam}${bizParam}${locationParam}${brandParam}${otherParam}`;
};

module.exports = {
    getWebviewUrl,
};
